<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="30vw"
    :close-on-click-modal="false"
    @close="close"
    :append-to-body="true"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="0.8rem">
      <el-form-item label="姓名">
        <el-input v-model.trim="form.xingming" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model.trim="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="!form.id">
        <el-input
          v-model.trim="form.password"
          show-password
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="form.xingbie">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="角色">
        <el-select
          v-model="form.jueseID"
          placeholder="请选择"
          @change="selectJS"
        >
          <el-option
            v-for="(item, index) in jueselist"
            :key="index"
            :label="item.juesemc"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="机构">
        <el-cascader
          :options="treeList"
          v-model="form.jigouID"
          @change="selectJG"
          :props="{
            checkStrictly: true,
            value: 'id',
            label: 'jigoumc',
            children: 'childList',
            emitPath: false,
          }"
          clearable
        ></el-cascader>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { addUser, EditUser } from '@/api/userManagement'

  export default {
    name: 'UserManagementEdit',
    props: {
      jueselist: {
        type: Array,
        default: function () {
          return []
        },
      },
      treeList: {
        type: Array,
        default: function () {
          return []
        },
      },
    },
    data() {
      return {
        form: {},
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            {
              min: 3,
              max: 12,
              message: '长度在 3 到12 个字符',
              trigger: 'blur',
            },
          ],
          password: [
            { required: true, message: '请输入用户密码', trigger: 'blur' },
            {
              min: 6,
              max: 20,
              message: '长度在 6 到 20 个字符',
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },

    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '新增'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      selectJS(e) {
        let item = this.jueselist.find((item) => item.id == e)
        this.form.juese = item.juesemc
      },
      selectJG(e) {
        this.form.jigou = this.$util.findNodeById(this.treeList, e)?.jigoumc
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (!this.form.id) {
              const { msg } = await addUser(this.form)
              this.$baseMessage(msg, 'success')
            } else {
              delete this.form.password
              const { msg } = await EditUser(this.form)
              this.$baseMessage(msg, 'success')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
